package cn.itzzx.lagou.consumer;


import cn.hutool.core.util.RandomUtil;
import cn.itzzx.lagou.consumer.bean.ConsumerComponent;
import cn.itzzx.lagou.consumer.cache.MonitorCachce;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;

public class DubboConsumerMain {

    private static final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

    private static final ExecutorService executorService1 = Executors.newFixedThreadPool(5);


    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent bean = context.getBean(ConsumerComponent.class);
        printTime();

        while (true) {
            System.in.read();
            for (int i = 0; i < 10; i++) {
                executorService1.submit(() -> {
                    run(bean);
                });
            }

        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("cn.itzzx.lagou")
    @EnableDubbo
    static class ConsumerConfiguration {

    }

    public static void run(ConsumerComponent bean) {
        int i = RandomUtil.randomInt(0, 3);
        if (i == 0) {
            bean.engine();
        }
        if (i == 1) {
            bean.wheel();
        }
        if (i == 2) {
            bean.run();
        }
    }


    public static void printTime() {

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Map<String, List<MonitorCachce.TimeConsuming>> monitor = MonitorCachce.getMonitor();
                long bi = System.currentTimeMillis();
                for (Map.Entry<String, List<MonitorCachce.TimeConsuming>> map1 :
                        monitor.entrySet()) {
                    String key = map1.getKey();
                    List<MonitorCachce.TimeConsuming> value = map1.getValue();
                    int index = value.size();

                    for (int i = (index - 1); (i >= 0 && i < index); i--) {
                        MonitorCachce.TimeConsuming timeConsuming = value.get(i);
                        if ((bi - timeConsuming.getInsertTime()) / 1000 - 60 > 60) {
                            return;
                        }
                        System.out.println("method: " + key + " 耗时时间：" + timeConsuming.getTimeConsuming() + "(s)");
                    }

                }
            }
        }, 0, 3000);
    }
}
